package com.sl.crawler.common.utils.file;

import org.apache.commons.lang.StringUtils;

/**
 * Created by shiling on 2017/6/1.
 */
public class FilePathUtils {
    /**
     * 获取web程序的根目录
     *
     * @return
     */
    public final static String getWebAppRootPath() {
        String webAppRootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        webAppRootPath = StringUtils.removeStart(webAppRootPath, "file:/");
        return webAppRootPath;
    }

    /**
     * 获取绝对路径
     *
     * @return
     */
    public final static String getAbsolutePath(String path) {
        // 不是绝对路径,加上
        if (StringUtils.indexOf(path, ":") == -1 || !StringUtils.startsWith(path, "/")) {
            path = FilePathUtils.getWebAppRootPath() + path;
        }
        return path;
    }
}
